package com.whisperarts.financer.app.dialogs;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

import com.whisperarts.financer.adapters.CategoryAdapter;
import com.whisperarts.financer.app.utils.UIUtils;
import com.whisperarts.financer.app.widgets.IncomeButton;
import com.whisperarts.financer.model.transaction.Category;

/**
 * @author AD
 *
 */
public class AddCategoryDialog extends Dialog {

	protected Text textName;
	
	private FormToolkit toolkit;

	private String shellTitle;	

	private final Shell parentShell;
	
	protected Combo comboCategories;

	private Category category;

	private String name;

	private int parentId;
	
	protected CategoryAdapter adapter;

	protected IncomeButton button;

	public AddCategoryDialog(Shell parentShell) {
		super(parentShell);
		this.parentShell = parentShell;
		adapter = new CategoryAdapter();
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		shellTitle = "Add new category";
		newShell.setText(shellTitle);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		toolkit = new FormToolkit(parent.getDisplay());
		
		Form form = toolkit.createForm(parent);
		form.setText("New category");
		toolkit.decorateFormHeading(form);
		toolkit.paintBordersFor(form.getBody());

		TableWrapLayout layout = new TableWrapLayout();
		layout.numColumns = 1;
		form.getBody().setLayout(layout);

		Composite composite = toolkit.createComposite(form.getBody());

		toolkit.adapt(composite);

		GridLayout glayout = new GridLayout(2, false);
		composite.setLayout(glayout);

		toolkit.createLabel(composite, "&Name:");
		textName = new Text(composite, SWT.BORDER);
		textName.setLayoutData(new GridData(GridData.FILL, GridData.FILL,
				true, false));

		toolkit.createLabel(composite, "&Subcategory of:");

		Composite incomeComposite = toolkit.createComposite(composite);
		incomeComposite.setLayout(new GridLayout(2, false));
		
		comboCategories = new Combo(incomeComposite, SWT.READ_ONLY);
		comboCategories.setItems(adapter.getStructuredTitles(getExcludeCategory()));
		comboCategories.select(0);
		comboCategories.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				button.setIncome( adapter.getCategoryByPosition(comboCategories.getSelectionIndex()).isIncome() );

			}
		});
		comboCategories.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		button = new IncomeButton(incomeComposite, SWT.NONE);

		fillData();
		
		return composite;
	}

	protected Category getExcludeCategory() {
		return null;
	}

	
	protected void fillData() {
	}

	@Override
	protected void okPressed() {		
		name = textName.getText();
		
		if ("".equals(name.trim())) {
			MessageDialog.openError(getShell(), "Invalid Title",
			"Title field must not be blank.");
			return;
		}
		
		if (isNameExists(name)) {
			MessageDialog.openError(parentShell, "Error", "Category " + name + " already exist. Choose another name");
			return;
		}
	
		parentId = adapter.getCategoryByPosition(comboCategories.getSelectionIndex()).getId();
		
		createCategory();
		super.okPressed();
	}

	protected boolean isNameExists(String name) {
		if (UIUtils.checkCategoryName(name)) {
			return true;
		}
		return false;
	}
	
	protected void createCategory() {
		category = getCategoryToCreate();
		category.setName(name);
		category.setIncome(button.isIncome());
		category.setParentId(parentId);
	}

	protected Category getCategoryToCreate() {
		return new Category();
	}

	public Category getCategory() {		
		return category;
	}
	
	public CategoryAdapter getAdapter() {
		return adapter;
	}

}
